using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.Collections;
using System.Collections.Specialized;

namespace PoshHttp
{
   [Cmdlet(VerbsData.ConvertTo, "Hashtable", DefaultParameterSetName="Strings")]
   public class ConvertToHashtableCommand : PSCmdlet
   {

      #region Parameters
      [Parameter(Position = 0,
         Mandatory = false,
         ValueFromPipelineByPropertyName = true,
         ParameterSetName="Strings",
         HelpMessage = "Args")]
      [ValidateNotNullOrEmpty]
      public string[] Arguments
      {
        get { return _arguments; }
        set { _arguments = value; }
      }
      string[] _arguments;

      [Parameter(Position = 0,
         Mandatory = false,
         ValueFromPipelineByPropertyName = true,
         ParameterSetName="NVC",
         HelpMessage = "Http Query String")]
      [ValidateNotNullOrEmpty]
      public NameValueCollection Values
      {
         get { return _values; }
         set { _values = value; }
      }
      NameValueCollection _values;
      #endregion

      protected override void ProcessRecord()
      {
         switch (ParameterSetName)
         {
            case "Strings":
               {
                  // if there's only one string and doesn't start with / or - 
                  // then it's a query string...
                  if (_arguments.Length == 1 &&
                     !(_arguments[0].Trim().StartsWith("/")
                     || _arguments[0].Trim().StartsWith("/")))
                  {
                     WriteObject(Utility.ParseQueryString(_arguments[0]));
                  }
                  else
                  {
                     WriteObject(Utility.ArgumentsToHashtable(_arguments));
                  }

               } break;
            case "NVC":
               {
                  WriteObject(Utility.ToHashtable(_values));
               } break;
         }
         
      }
   }
}
